﻿/*************************   Auto Generated Repository Code For PatientInsurance table     Generated @ 4/27/2013 9:57:48 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class PatientInsuranceRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an PatientInsurance by its PatientInsuranceID (Primary Key)
        /// </summary>
        /// <param name="PatientInsuranceID">The ID of the <see cref="PatientInsurance"/> to retrieve</param>
        /// <returns>A <see cref="PatientInsurance"/> object</returns>
        public BusinessObjects.PatientInsurance Select(int PatientInsuranceID)
        {
            PatientInsurance retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.PatientInsurances where x.PatientInsuranceID == PatientInsuranceID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of PatientInsurance objects
        /// </summary>
        /// <returns>A List of type <see cref="PatientInsurance"/></returns>
        public List<BusinessObjects.PatientInsurance> SelectAll()
        {
            List<BusinessObjects.PatientInsurance> retObject = new List<BusinessObjects.PatientInsurance>();

            using (dB = new PracticeSimpleDataContext())
            {
                var PatientInsuranceList = (from x in dB.PatientInsurances select x).ToList();

                foreach (PatientInsurance obj in PatientInsuranceList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a PatientInsurance Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public BusinessObjects.PatientInsurance Save(BusinessObjects.PatientInsurance businessObject)
        {
            PatientInsurance patientInsurance = ConvertToDataObject(businessObject);
            using (dB = new PracticeSimpleDataContext())
            {
                dB.PatientInsurances.InsertOnSubmit(patientInsurance);
                dB.SubmitChanges();
            }

            return ConvertToBusinessObject(patientInsurance);
        }

        /// <summary>
        /// Accepts the Primary Key of a PatientInsurance, finds it in the database and deletes it
        /// </summary>
        /// <param name="PatientInsuranceID">The ID of the <see cref="PatientInsurance"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int PatientInsuranceID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.PatientInsurance obj = (from x in dB.PatientInsurances where x.PatientInsuranceID == PatientInsuranceID select x).SingleOrDefault();
                dB.PatientInsurances.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a PatientInsurance data object to a PatientInsurance business object
        /// </summary>
        /// <returns>A business object of type <see cref="PatientInsurance"/></returns>
        public BusinessObjects.PatientInsurance ConvertToBusinessObject(DataLayer.PatientInsurance dataObject)
        {
            BusinessObjects.PatientInsurance businessObject = new BusinessObjects.PatientInsurance();

            businessObject.PatientInsuranceID = dataObject.PatientInsuranceID;
            businessObject.PatientID = dataObject.PatientID;
            businessObject.InsuranceName = dataObject.InsuranceName;
            businessObject.Address1 = dataObject.Address1;
            businessObject.Address2 = dataObject.Address2;
            businessObject.City = dataObject.City;
            businessObject.State = dataObject.State;
            businessObject.ZIP = dataObject.ZIP;
            businessObject.MainPhone = dataObject.MainPhone;
            businessObject.Fax = dataObject.Fax;
            businessObject.Notes = dataObject.Notes;
            businessObject.PolicyNumber = dataObject.PolicyNumber;
            businessObject.GroupNumber = dataObject.GroupNumber;
            businessObject.NetworkNumber = dataObject.NetworkNumber;
            businessObject.CoverageDate = dataObject.CoverageDate;


            return businessObject;

        }

        /// <summary>
        /// Converts a PatientInsurance business object to a PatientInsurance data object
        /// </summary>
        /// <returns>A data object of type <see cref="PatientInsurance"/></returns>
        public DataLayer.PatientInsurance ConvertToDataObject(BusinessObjects.PatientInsurance businessObject)
        {
            DataLayer.PatientInsurance dataObject = new DataLayer.PatientInsurance();

            dataObject.PatientInsuranceID = businessObject.PatientInsuranceID;
            dataObject.PatientID = businessObject.PatientID;
            dataObject.InsuranceName = businessObject.InsuranceName;
            dataObject.Address1 = businessObject.Address1;
            dataObject.Address2 = businessObject.Address2;
            dataObject.City = businessObject.City;
            dataObject.State = businessObject.State;
            dataObject.ZIP = businessObject.ZIP;
            dataObject.MainPhone = businessObject.MainPhone;
            dataObject.Fax = businessObject.Fax;
            dataObject.Notes = businessObject.Notes;
            dataObject.PolicyNumber = businessObject.PolicyNumber;
            dataObject.GroupNumber = businessObject.GroupNumber;
            dataObject.NetworkNumber = businessObject.NetworkNumber;
            dataObject.CoverageDate = businessObject.CoverageDate;


            return dataObject;
        }
    }
}


